package control;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.service.ProjectService;
import model.service.SmsService;
import model.vo.MemberVO;
import model.vo.MplistVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class SmsController extends MultiActionController {
	private SmsService smsService;
	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}
	private ProjectService projectService;
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	//메인 화면 접속
	public ModelAndView mainPage(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		System.out.println("mainPage");
		String ip = request.getRemoteAddr();
		String G_Connect = request.getParameter("G_Connect");
		//저장된 세션을 이용
		HttpSession session = request.getSession(false);
		String logId="";
		if(session!=null && session.getAttribute("USER_ID")!=null){
			//MemberVO mvo = (MemberVO) session.getAttribute("mvo");
			String USER_ID=(String) session.getAttribute("USER_ID");
			//MplistVO pvo=null;
			//프로젝트 리스트 받아옴
			
			
			if(USER_ID!=null&&USER_ID!=""){
				logId=USER_ID;
			}
			//if (G_Connect == null) {
			//} else if (G_Connect.equals("1")) {
				//최초 접속시 사용자의 ip와 접속시간을 저장
				//smsService.insertLog(logId, G_Connect, ip);
				//최초 접속시 마지막 프로젝트 회차를 세션에 저장
				//List mplist=projectService.mplist();
				//int pro_id=0;
				/*
				session.setAttribute("mplist", mplist);
				//System.out.println(mplist.size());
				//프로젝트 회차가 없지 않으면
				if(mplist.size()!=0){
					//pvo에 마지막 회차정보를 저장
					pvo=(MplistVO) mplist.get(mplist.size()-1);
					pro_id=pvo.getPro_id();
				}
				//pvo를 세션에 저장(값이 없으면 null, 있으면 마지막 회차 정보)
				session.setAttribute("lastProId", pro_id);
				*/
			//}
			
		}
		List list=null;
		if(session!=null && session.getAttribute("lastProId")!=null){
			int pro_id=(Integer) session.getAttribute("lastProId");
			list=projectService.proSelectCount(pro_id);
			mv.setViewName("sms_main");
		}else{
			//결과를 리스트에 담음
			list=projectService.mplist(logId);
			//리스트라는 객체에 list를 담아 보넴(request.setAttribute와 동일)
			mv.setViewName("base_layout");
		}
		System.out.println(list);
		mv.addObject("list",list);
		return mv;
	}
	//로그 히스토리 (미구현)
	/*
	public ModelAndView logHistory(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("logHistory");
		return mv;
	}
	*/
}
